part of "../time.dart";

extension ExtraFunctionDuration on Duration {
  double operator /(Duration other) => inMicroseconds / other.inMicroseconds;
  Duration operator *(int number) =>
      Duration(microseconds: inMicroseconds * number);

  /// Duration之间相除，取余也应该是Duration
  Duration operator %(Duration other) {
    int durationInMs = inMicroseconds % other.inMicroseconds;
    return Duration(microseconds: durationInMs);
  }

  // 取整在Duration中已经被定义，无法重新定义
  // int operator ~/(Duration other) => inMicroseconds ~/ other.inMicroseconds;

  /// 占据一天的多少，1为一整天
  double get ratioByDay => inMicroseconds / Duration.microsecondsPerDay;
}

extension Format on Duration {
  int get days => inDays;
  int get hours => inHours - inDays * 24;
  int get minutes => inMinutes - inHours * 60;
  int get seconds => inSeconds - inMinutes * 60;

  /// 参照"format规则.md"
  String format(String pattern) => pattern
      .formatDay(days)
      .formatHour(hours)
      .formatMinute(minutes)
      .formatSecond(seconds);
}
